package com.thx.addressbook.dao;

import com.thx.addressbook.AddressBookPurviewType;
import com.thx.addressbook.AddressBookType;
import com.thx.addressbook.model.AddressbookRole;
import com.thx.common.orm.PaginationDao;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class AddressbookRoleDao extends PaginationDao<AddressbookRole> {

  private static final Logger logger = LoggerFactory.getLogger(AddressbookRoleDao.class);

  /**
   * 根据授权类型、授权对象查询权限集合.
   * 
   * @param parentId 授权id，不能为空
   * @param parentType 授权类型，不能为空
   * @return 权限集合
   */
  public List<AddressbookRole> findByPIdAndPType(Long parentId, String parentType) {
    List<AddressbookRole> list = new ArrayList<AddressbookRole>();
    if (parentId != null && parentId != 0L && StringUtils.isNotBlank(parentType)) {
      String hql = "from AddressbookRole where parentType=? and parentId = ? ";
      List<Object> objs = new ArrayList<Object>();
      objs.add(parentType);
      objs.add(parentId);
      list = createQuery(hql).list();
    }
    return list;
  }

  /**
   * 根据人员Id、人员所属部门id、人员角色id查询人员所有分类授权对象.
   * 
   * @param perId 人员id,不能为空
   * @param depId 所属部门id,可以为空
   * @param role 所属角色id,可以为空
   * @return 分类权限集合
   */
  public List<AddressbookRole> findCategryByPerIdOrDepOrRole(String perId, String depId,
      String role) {
    List<AddressbookRole> list = new ArrayList<AddressbookRole>();
    List<Object> objs = new ArrayList<Object>();
    if (StringUtils.isNotBlank(perId)) {
      String hql =
          "from AddressbookRole where (parentType=? and parent_type =? and parent_id =? ) ";
      objs.add(AddressBookType.CATEGORY);
      objs.add(perId);
      objs.add(AddressBookPurviewType.PER);
      if (StringUtils.isNotBlank(depId)) {
        hql += " or (parentType=? parent_type =? and parent_id =?)";
        objs.add(AddressBookType.CATEGORY);
        objs.add(depId);
        objs.add(AddressBookPurviewType.DEPARTMENT);
      }
      if (StringUtils.isNotBlank(role)) {
        hql += " or (parentType=? parent_type =? and parent_id =?)";
        objs.add(AddressBookType.CATEGORY);
        objs.add(role);
        objs.add(AddressBookPurviewType.ROLE);
      }
      list = createQuery(hql, objs.toArray()).list();
    }
    return list;
  }

  /**
   * 根据人员Id、人员所属部门id、人员角色id查询人员所有分组授权对象.
   * 
   * @param perId 人员id,不能为空
   * @param depId 所属部门id,可以为空
   * @param role 所属角色id,可以为空
   * @return 分组权限集合
   */
  public List<AddressbookRole> findGroupByPerIdOrDepOrRole(String perId, String depId,
      String role) {
    List<AddressbookRole> list = new ArrayList<AddressbookRole>();
    List<Object> objs = new ArrayList<Object>();
    if (StringUtils.isNotBlank(perId)) {
      String hql =
          "from AddressbookRole where (parentType=? and parent_type =? and parent_id =? ) ";
      objs.add(AddressBookType.GROUP);
      objs.add(perId);
      objs.add(AddressBookPurviewType.PER);
      if (StringUtils.isNotBlank(depId)) {
        hql += " or (parentType=? parent_type =? and parent_id =?)";
        objs.add(AddressBookType.GROUP);
        objs.add(depId);
        objs.add(AddressBookPurviewType.DEPARTMENT);
      }
      if (StringUtils.isNotBlank(role)) {
        hql += " or (parentType=? parent_type =? and parent_id =?)";
        objs.add(AddressBookType.GROUP);
        objs.add(role);
        objs.add(AddressBookPurviewType.ROLE);
      }
      list = createQuery(hql, objs.toArray()).list();
    }
    return list;
  }

  // public List<AddressbookRole> queryAllByCondition(AddressbookRole info,
  // final int start, final int limit) {
  // String hql = getHql(info);
  // List<AddressbookRole> list =
  // createQuery(hql).setFirstResult(start).setMaxResults(limit).list();
  // return list;
  // }
  //
  // public int queryCount(AddressbookRole info) {
  // String hql = "select count(*) from AddressbookRole f where 1=1";
  // if (info.getParentId() != null && info.getParentId() != 0) {
  // hql += " and f.parentId = " + info.getParentId();
  // }
  // List list = createQuery(hql).list();
  // if (list.size() > 0) {
  // Long count = (Long) list.get(0);
  // return new Integer(count + "");
  // }
  // return 0;
  // }
  //
  // public String getHql(AddressbookRole info) {
  // String hql = "from AddressbookRole c where 1=1";
  // if (info.getParentId() != null && info.getParentId() != 0) {
  // hql += " and c.parentId = " + info.getParentId();
  // }
  // return hql;
  // }
  //
  //
  // /**
  // * 根据视图 view 查找 建立日期 : May 15, 2013 6:36:18 PM<br>
  // * 作者: 孙耀飞<br>
  // * 模块 : <br>
  // * 描述 : <br>
  // * 1 <br>
  // * 2 <br>
  // */
  // public List<AddressbookRole> findByView(AddressbookRoleView roleView) {
  // List<AddressbookRole> list = new ArrayList<AddressbookRole>();
  // String hql = "from AddressbookRole a where 1=1";
  // if (roleView.getObjId() != null && roleView.getObjId() != 0L) {
  // hql += " and a.objId = " + roleView.getObjId();
  // }
  // if (StringUtils.isNotBlank(roleView.getObjType())) {
  // hql += " and a.objType = " + roleView.getObjType();
  // }
  // if (roleView.getParentId() != null && roleView.getParentId() != 0L) {
  // hql += " and a.parentId = " + roleView.getParentId();
  // }
  // if (StringUtils.isNotBlank(roleView.getParentType())) {
  // hql += " and a.parentType = " + roleView.getParentType();
  // }
  // if (roleView.getEnterpriseId() != null && roleView.getEnterpriseId() !=
  // 0) {
  // hql += " and a.enterpriseId = " + roleView.getEnterpriseId();
  // }
  // list = createQuery(hql).list();
  // return list;
  // }
}
